package com.fruit.task;

import com.fruit.bean.entity.fruit.FruitProfitInfo;
import com.fruit.bean.entity.fruit.FruitStockInfo;
import com.fruit.service.fruit.FruitProfitService;
import com.fruit.service.fruit.FruitSellRequestService;
import com.fruit.service.fruit.FruitStockService;
import com.fruit.utils.BigDecimalUtils;
import com.fruit.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * Description:
 *  收益计算定时任务
 *  每天0点执行，根据库存水果重量计算管理费收益
 * @author 轩辚
 * @date 2021/5/9 22:50
 */
@Component
@Slf4j
public class FruitProfitTask {

    @Autowired
    private FruitStockService stockService;

    @Autowired
    private FruitSellRequestService sellRequestService;

    @Autowired
    private FruitProfitService profitService;

    /** 为了方便暂时写死。默认每千克水果一天收费1毛钱 */
    private static final double PROFIT_MONEY = 0.1;

    @Scheduled(cron = "0 0 0 * * ?")
//    @Scheduled(cron = "0/5 * * * * ?")
    public void profitTask(){
        log.info("更新库存收益"+ DateUtil.getTime());
        List<FruitStockInfo> stockInfoList = stockService.queryAll();
        for(FruitStockInfo stockInfo : stockInfoList){
            if(stockInfo.getFruitStockNumber() == 0){
                continue;
            }
            FruitProfitInfo profitInfo = new FruitProfitInfo();
            profitInfo.setCreateTime(new Date());
            profitInfo.setProfitType(0);
            profitInfo.setStockInfoId(stockInfo.getId());
            profitInfo.setProfit(BigDecimalUtils.mul(PROFIT_MONEY,
                    stockInfo.getFruitStockNumber()).doubleValue());
            profitService.insert(profitInfo);
        }
    }
}
